package com.cyt.web.controller.sys;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysDict;
import com.cyt.sys.model.vo.SysDictVo;
import com.cyt.sys.service.SysDictService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 基础部分-数据字典控制层
 *
 * @author shengQiang yu
 * @date 2019-04-03 10:29
 */
@RestController
public class SysDictController extends SysBaseController {

    @Resource
    private SysDictService sysDictService;


    /**
     * 基础部分-数据字典统一跳转处理
     *
     * @author shengQiang yu
     * @date 2019-04-03 10:29
     */
    @RequestMapping("/sys/dict/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 左边分页查询
     *
     * @param sysDictVo 对象
     * @param page      当前页面
     * @param limit     页面数量
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2018-11-29 15:43
     */
    @BaseLog(module = "系统模块", classz = "字典管理", methods = "分页查询", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysDict/page", method = RequestMethod.POST)
    public ReturnResult page(SysDictVo sysDictVo, int limit, int page) {
        ReturnResult returnResult = sysDictService.pageLeft(sysDictVo, limit, page);
        return returnResult;
    }

    /**
     * 添加或修改
     *
     * @param request   对象
     * @param sysDictVo 对象
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2018-11-29 15:43
     */
    @BaseLog(module = "系统模块", classz = "字典管理", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysDict/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, SysDictVo sysDictVo) {
        ReturnResult result = sysDictService.addOrUpdate(sysDictVo, acceptCurrentUser(request));
        return result;
    }

    /**
     * 跳转编辑页面
     *
     * @param id 主键
     * @author shengQiang yu
     * @date 2018-11-29 15:43
     */
    @RequestMapping(value = "/sysDict/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id) {
        ModelAndView view = new ModelAndView("/sys/dict/dictEdit");
        if (id != null && !"".equals(id)) {
            SysDict sysDict = sysDictService.selectByPrimaryKey(id);
            view.addObject(sysDict);
        }
        return view;
    }

    /**
     * 根据主键id集合软删除
     *
     * @param id 主键id集合，多个以‘，’隔开
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2018-11-29 15:43
     */
    @BaseLog(module = "系统模块", classz = "字典管理", methods = "删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysDict/removeByIds", method = RequestMethod.GET)
    public ReturnResult removeByIds(HttpServletRequest request, String id) {
        return sysDictService.removeByIds(id, acceptCurrentUser(request));
    }

    /**
     * 跳转固定字典数据页面
     *
     * @param code 字典码类型
     * @return ReturnResult 对象
     * @author liufu zhu
     * @date 2018-11-29 15:43
     */
    @RequestMapping(value = "/sysDict/getDictContent", method = RequestMethod.GET)
    public ModelAndView getDictContent(String code) {
        ModelAndView view = new ModelAndView("/sys/dictContent/dictContentList");
        view.addObject("dictType", code);
        return view;
    }

    /**
     * 跳转动态字典数据页面
     *
     * @param code 字典码类型
     * @return ReturnResult 对象
     * @author liufu zhu
     * @date 2018-11-29 15:43
     */
    @RequestMapping(value = "/sysDict/getDictContentRun", method = RequestMethod.GET)
    public ModelAndView getDictContentRun(String code) {
        ModelAndView view = new ModelAndView("/sys/dictContent/dictContentRunList");
        String url = null;
        if (StringUtils.isNotEmpty(code)) {
            SysDict sysDict = sysDictService.listByCode(code);
            url = sysDict.getUrl();
        }
        view.addObject("dictType", code);
        view.addObject("url", url);
        return view;
    }

    /**
     * list查询
     *
     * @param sysDictVo 参数传入对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019/4/16 10:00
     */
    @RequestMapping(value = "/sysDict/list", method = RequestMethod.POST)
    public ReturnResult list(SysDictVo sysDictVo) {
        return new ReturnResult(sysDictService.listExample(sysDictVo));
    }


    /**
     * 获取某个code的List
     *
     *
     * @param
     * @param
     * @param
     * @param
     * @return
     * @author Jun Han
     * @date 2019/12/2 14:49
     */
    @BaseLog(module = "系统模块", classz = "字典管理", methods = "获取所有考试的科目", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysDict/AllSublist", method = RequestMethod.POST)
    public ReturnResult getAllTeacherList(String keyWord) {
        SysDictVo sysDictVo = new SysDictVo();
        sysDictVo.setCode("testSubjectType");
        sysDictVo.setName(keyWord);
        ReturnResult returnResult = sysDictService.selector(sysDictVo);
        return returnResult;
    }

}
